package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.pass.PassInfo;
import com.yanqu.road.entity.config.pass.PassPointInfo;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PassDaoImpl extends TempDao {

	public Map<Integer, PassInfo> getPassInfoMap() {
		Map<Integer, PassInfo> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_pass order by id";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				PassInfo data = new PassInfo();
				data.setId(rs.getInt("id"));
				data.setBoss(rs.getBoolean("type"));
				data.setBaseInspireConsume(new BigInteger(rs.getString("baseInspireConsume")));
				data.setReward(PropertyHelper.parseStringToProperty(rs.getString("rewards")));

				String[] bloodArr = rs.getString("moneyList").split("\\|");
				String[] abilityArr = rs.getString("strengthList").split("\\|");
				String[] rewardArr = rs.getString("dropRewardList").split("\\|");
				List<PassPointInfo> passPointInfoList = new ArrayList<>();
				for (int i = 0; i < bloodArr.length; i++) {
					PassPointInfo pointPointInfo = new PassPointInfo();
					pointPointInfo.setAbility(new BigInteger(abilityArr[i]));
					pointPointInfo.setBlood(new BigInteger(bloodArr[i]));
					pointPointInfo.setReward(PropertyHelper.parseStringToProperty(rewardArr[i]));
					passPointInfoList.add(pointPointInfo);
				}

				data.setPassPointInfoList(passPointInfoList);
				dataMap.put(data.getId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}
}
